package com.ranjiaqi.thread08;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SubThread
 * @Description TODO
 * @Author hp
 * @Date 2021/6/28 18:55
 * @Version 1.0
 **/
public class SubThread implements Runnable {
    private int ticket = 100;
    private Lock lock = new ReentrantLock();

    public  void sell(){
        lock.lock();
        try {
            if (ticket > 0) {
                Thread.sleep(5);
                System.out.println(Thread.currentThread().getName() + "售出第" + ticket-- + "张票");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }

    @Override
    public void run() {
        while (true)
            sell();
    }
}

